package com.company.phonebook;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ShowCurrentLocation extends MapActivity {		
	/** Called when the activity is first created. */
	GeoPoint currentPoint;
	MapController mapViewController;
	@Override
	public void onCreate(Bundle savedInstanceState) {		
	    super.onCreate(savedInstanceState);	    
	    setContentView(R.layout.show_current_location);	    
	    
	    MapView mapView = (MapView) findViewById(R.id.map_view);
	    mapView.setBuiltInZoomControls(true);	   
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    
	    Drawable currentLocationMarker = this.getResources().getDrawable(R.drawable.current_marker);
	    MapItemizedOveraly itemizedOverlay = new MapItemizedOveraly(currentLocationMarker);
	    
	    Intent intent = getIntent();
	    double lat = intent.getDoubleExtra("latitude",0);
	    double lon = intent.getDoubleExtra("longitude",0);	
	    String provider = intent.getStringExtra("provider");
	    	    
	    this.setTitle("Location Manager running in " + "'" + provider + "'");
	    currentPoint = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
	    
	    mapViewController = mapView.getController();
	    mapViewController.animateTo(currentPoint);
	    mapViewController.setZoom(18);
	    	    
	    GeoPoint point = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
	    OverlayItem overlayitem = new OverlayItem(point, null, null);
	    itemizedOverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedOverlay);	
	    addMenu.mapLoadingDialog.dismiss();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.locationmap_context_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.back_to_add:
				this.finish();
				break;
			case R.id.back_to_location:
				mapViewController.animateTo(currentPoint);
			    mapViewController.setZoom(18);
			    break;				
		}
		return super.onOptionsItemSelected(item);
	}

}
